home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GENCONF.C < prev    next >
C/C++ Source or Header  |  1993-05-20  |  8KB  |  285 lines

  1. /* Copyright (C) 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* genconf.c */
  20. /* Generate configuration files for Ghostscript */
  21. #include <ctype.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>        /* for calloc */
  24. #include <string.h>
  25.  
  26. /*
  27.  * This program generates a set of configuration files for Ghostscript.
  28.  * Everything it does could be done by a shell program, except that
  29.  * (1) Unix shells are not compatible from one system to another,
  30.  * (2) the DOS shell is not equal to the task,
  31.  * (3) the VMS shell is radically different from all others.
  32.  *
  33.  * Usage:
  34.  *    genconf [@]xxx.dev* [-f gconfigf.h] [-h gconfig.h]
  35.  *      [-l lib.tr] [-o obj.tr] [-u ld.tr] [-w objw.tr]
  36.  */
  37.  
  38. /* This program has to use K&R C, so it will work with old compilers. */
  39. #undef const
  40. #define const /* */
  41. #define P1(a1) /* */
  42. #define P2(a1,a2) /* */
  43. #define P3(a1,a2,a3) /* */
  44. /* Unfortunately, we have to hack up the function definitions by hand. */
  45.  
  46. /* Structures for accumulating information */
  47. typedef struct string_list_s {
  48.     int max_count, count;
  49.     char **strings;
  50. } string_list;
  51. typedef struct config_s {
  52.     string_list resources;
  53.     string_list devs;
  54.     string_list fonts;
  55.     string_list libs;
  56.     string_list objs;
  57. } config;
  58. static const config init_config = {
  59.     { 200 }, { 100 }, { 200 }, { 100 }, { 200 }
  60. };
  61.  
  62. /* Forward definitions */
  63. int alloc_list(P1(string_list *));
  64. int read_dev(P2(config *, const char *));
  65. int read_token(P3(char *, int, FILE *));
  66. int add_entry(P3(config *, char *, const char *));
  67. void sort_uniq(P1(string_list *));
  68. void write_list(P3(FILE *, const string_list *, const char *));
  69.  
  70. main(argc, argv) int argc; char *argv[];
  71. {    config conf;
  72.     int i;
  73.  
  74.     /* Allocate string lists. */
  75.     conf = init_config;
  76.     alloc_list(&conf.resources);
  77.     alloc_list(&conf.devs);
  78.     alloc_list(&conf.fonts);
  79.     alloc_list(&conf.libs);
  80.     alloc_list(&conf.objs);
  81.  
  82.     /* Process command line arguments. */
  83.     for ( i = 1; i < argc; i++ )
  84.     {    const char *arg = argv[i];
  85.         FILE *out;
  86.         if ( *arg != '-' )
  87.         {    read_dev(&conf, arg);
  88.             continue;
  89.         }
  90.         if ( i == argc - 1 )
  91.         {    fprintf(stderr, "Missing file name after %s.\n",
  92.                 arg);
  93.             exit(1);
  94.         }
  95.         out = fopen(argv[++i], "w");
  96.         if ( out == 0 )
  97.         {    fprintf(stderr, "Can't open %s for output.\n",
  98.                 argv[i]);
  99.             exit(1);
  100.         }
  101.         switch ( arg[1] )
  102.         {
  103.         case 'f':
  104.             fputs("/* This file was generated automatically by Ghostscript (genconf.c). */\n", out);
  105.             write_list(out, &conf.fonts,
  106.                    "font_(\"0.font_%s\",gsf_%s,zf_%s)\n");
  107.             break;
  108.         case 'h':
  109.             fputs("/* This file was generated automatically by Ghostscript (genconf.c). */\n", out);
  110.             fputs("#include \"gsconfig.h\"\n", out);
  111.             write_list(out, &conf.devs, "device__(gs_%s_device)\n");
  112.             sort_uniq(&conf.resources);
  113.             write_list(out, &conf.resources, "%s\n");
  114.             break;
  115.         case 'l':
  116.             write_list(out, &conf.libs, "%s+\n");
  117.             break;
  118.         case 'o':
  119.             sort_uniq(&conf.objs);
  120.             write_list(out, &conf.objs, "%s+\n");
  121.             break;
  122.         case 'u':
  123.             sort_uniq(&conf.objs);
  124.             write_list(out, &conf.objs, "%s \\\n");
  125.             write_list(out, &conf.libs, "-l%s \\\n");
  126.             break;
  127.         case 'w':
  128.             sort_uniq(&conf.objs);
  129.             write_list(out, &conf.objs, "FILE %s\n");
  130.             break;
  131.         default:
  132.             fclose(out);
  133.             fprintf(stderr, "Unknown switch %s.\n", arg);
  134.             exit(1);
  135.         }
  136.         fclose(out);
  137.     }
  138.  
  139.     exit(0);
  140. }
  141.  
  142. /* Allocate and initialize a string list. */
  143. int
  144. alloc_list(list) string_list *list;
  145. {    list->count = 0;
  146.     list->strings =
  147.         (char **)calloc(list->max_count, sizeof(char *));
  148.     return 0;
  149. }
  150.  
  151. /* Read and parse a .dev file. */
  152. int
  153. read_dev(pconf, arg) config *pconf; const char *arg;
  154. {    FILE *in;
  155. #define max_token 32
  156.     char token[max_token];
  157.     int len;
  158.     if ( arg[0] == '@' )
  159.     {    in = fopen(arg + 1, "r");
  160.         if ( in == 0 )
  161.         {    fprintf(stderr, "Can't read %s.\n", arg + 1);
  162.             exit(1);
  163.         }
  164.         while ( (len = read_token(token, max_token, in)) > 0 )
  165.             read_dev(pconf, token);
  166.     }
  167.     else
  168.     {    char category[max_token];
  169.         in = fopen(arg, "r");
  170.         if ( in == 0 )
  171.         {    fprintf(stderr, "Can't read %s.\n", arg);
  172.             exit(1);
  173.         }
  174.         strcpy(category, "obj");
  175.         while ( (len = read_token(token, max_token, in)) > 0 )
  176.             add_entry(pconf, category, token);
  177.     }
  178. #undef max_token
  179.     fclose(in);
  180.     if ( len < 0 )
  181.     {    fprintf(stderr, "Token too long: %s.\n", token);
  182.         exit(1);
  183.     }
  184.     return 0;
  185. }
  186.  
  187. /* Read a token from a file. */
  188. int
  189. read_token(token, max_len, in) char *token; int max_len; FILE *in;
  190. {    int len = 0;
  191.     for ( ; ; ) 
  192.     {    char ch = fgetc(in);
  193.         token[len] = 0;
  194.         if ( feof(in) )
  195.             return len;
  196.         if ( isspace(ch) )
  197.         {    if ( len > 0 )
  198.                 return len;
  199.             continue;
  200.         }
  201.         if ( len == max_len )
  202.             return -1;    /* token too long */
  203.         token[len++] = ch;
  204.     }
  205. }
  206.  
  207. /* Add an entry to a configuration. */
  208. int
  209. add_entry(pconf, category, item) config *pconf; char *category; const char *item;
  210. {    if ( item[0] == '-' )        /* set category */
  211.         strcpy(category, item + 1);
  212.     else                /* add to current category */
  213.     {    char str[80];
  214.         const char *pat = "%s";
  215.         char *rstr;
  216.         string_list *list = &pconf->resources;
  217.         /* Handle a few resources specially: */
  218.         if ( !strcmp(category, "dev") )
  219.             list = &pconf->devs;
  220.         else if ( !strcmp(category, "font") )
  221.             list = &pconf->fonts;
  222.         else if ( !strcmp(category, "include") )
  223.         {    strcpy(str, item);
  224.             strcat(str, ".dev");
  225.             read_dev(pconf, str);
  226.             return 0;
  227.         }
  228.         else if ( !strcmp(category, "lib") )
  229.             list = &pconf->libs;
  230.         else if ( !strcmp(category, "obj") )
  231.             list = &pconf->objs;
  232.         /* Now handle all other resources. */
  233.         else if ( !strcmp(category, "fdev") )
  234.             pat = "file_device__(gs_fdev_%s)";
  235.         else if ( !strcmp(category, "oper") )
  236.             pat = "oper__(%s_op_defs)";
  237.         else if ( !strcmp(category, "oper2") )
  238.             pat = "oper2__(%s_op_defs)";
  239.         else if ( !strcmp(category, "ps") )
  240.             pat = "psfile__(\"%s.ps\")";
  241.         else
  242.         {    fprintf(stderr, "Unknown category %s.\n", category);
  243.             exit(1);
  244.         }
  245.         sprintf(str, pat, item);
  246.         rstr = malloc(strlen(str) + 1);
  247.         strcpy(rstr, str);
  248.         list->strings[list->count++] = rstr;
  249.     }
  250.     return 0;
  251. }
  252.  
  253. /* Sort and uniq an array of strings. */
  254. int
  255. strcmp_ptr(ps1, ps2) const void *ps1; const void *ps2;
  256. {    return strcmp(*(const char **)ps1, *(const char **)ps2);
  257. }
  258. void
  259. sort_uniq(list) string_list *list;
  260. {    char **strlist = list->strings;
  261.     int count = list->count;
  262.     char **from;
  263.     char **to;
  264.     int i;
  265.     if ( count == 0 )
  266.         return;
  267.     qsort((char *)strlist, count, sizeof(char *), strcmp_ptr);
  268.     for ( from = to = strlist + 1, i = 1; i < count; from++, i++ )
  269.       if ( strcmp(*from, to[-1]) )
  270.         *to++ = *from;
  271.     list->count = to - strlist;
  272. }
  273.  
  274. /* Write a list of strings using a template. */
  275. void
  276. write_list(out, list, template) FILE *out; const string_list *list; const char *template;
  277. {    int i;
  278.     for ( i = 0; i < list->count; i++ )
  279.     {    const char *str = list->strings[i];
  280.         /* The repetition of str is a hack to make the */
  281.         /* fonts template work. */
  282.         fprintf(out, template, str, str, str);
  283.     }
  284. }
  285.